package forms;

import helpers.ValidateHelper;

import java.net.URL;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import entities.Website;


public class WebsiteForm {
	public static TreeMap<String, Object> checkValid(HttpServletRequest request, Website website) {
		TreeMap<String, Object> form = new TreeMap<String, Object>();
		TreeMap<String, String> checkValid = new TreeMap<String, String>();
		
		String domain = (String) request.getParameter("domain");
		String title = (String) request.getParameter("title");
		String status = (String) request.getParameter("status");
		String sender = (String) request.getParameter("sender");
		String contentUrl = (String) request.getParameter("contentUrl");
		
		if (domain == null || domain.isEmpty()) {
			checkValid.put("domain", "Please input domain name.");
			website.setDomain("");
		} else {
			website.setDomain(domain);
		}
		
		if (title == null || title.isEmpty()) {
			checkValid.put("title", "Please input title for email.");
			website.setTitle("");
		} else {
			website.setTitle(title);
		}
		
		
		if (status == null || status.isEmpty()) {
			checkValid.put("status", "Status is not emtpy");
		} else {
			try {
				website.setStatus(Integer.parseInt(status));
			} catch (Exception e) {
				checkValid.put("status", "Status must be a number!");
			}
		}
		
		if (sender == null || sender.isEmpty()) {
			checkValid.put("sender", "Please input sender email.");
			website.setSender("");
		} else {
			website.setSender(sender);
		}
		
		if (contentUrl == null || contentUrl.isEmpty()) {
			checkValid.put("contentUrl", "Please input url content email.");
			website.setContentUrl("");
		} else {
			website.setContentUrl(contentUrl);
		}
		
		form.put("checkValid", checkValid);
		form.put("website", website);
		return form;
	}
	
}
